package com.zwl.smartplatform.controller;

import com.zwl.smartplatform.dto.R;
import com.zwl.smartplatform.pojo.Category;
import com.zwl.smartplatform.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.naming.Name;
import java.util.List;

@RestController
@RequestMapping("/categories")
public class CategoryController {

    private CategoryService categoryService;

    @Autowired
    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @PostMapping("/category")
    public R addCategory(@RequestBody Category category){
    categoryService.addCategory(category);
    return R.setOK();
    }

    @GetMapping("/categories")
    public R getAllCategory(){
        List<Category> categoryList = categoryService.selectAllCategories();
        return R.setOK(categoryList);
    }

    @GetMapping("/category/{id}")
    public R getCategoryById(@PathVariable Long id){
        Category category = categoryService.selectById(id);
        return R.setOK(category);
    }

    @GetMapping("/category/")
    public R getCategoryById(String cName){
        List<Category> categoryList = categoryService.selectCategoriesByNameLike(cName);
        return R.setOK(categoryList);
    }
    @DeleteMapping("/categories")
    public R deleteCategory(@RequestBody List<Long> ids){
        int result = categoryService.deleteCategoriesByIds(ids);
        return R.setOK();
    }
}
